package com.team.mp.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.mp.api.bo.DepartmentAddBo;
import com.team.mp.api.bo.DepartmentPageBo;
import com.team.mp.api.service.intf.DepartmentService;
import com.team.mp.mapper.dao.DepartmentDao;
import com.team.mp.mapper.dao.DoctorDao;
import com.team.mp.mapper.dto.DepartmentDto;
import com.team.mp.mapper.entity.Department;
import com.team.mp.mapper.entity.Doctor;
import com.team.mp.vo.R;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 千锋郑州Java学科欢迎你
 * @program: Tproject
 * @description: 业务接口实现层
 * @author: xiaopi
 * @create: 2023-03-27 11:47:49
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    /**
    *注入持久层对象*/
    @Resource
    private DepartmentDao dao;

    @Resource
    private DoctorDao doctorDao;



    /**
    * 新增内容*/
    @Override
    public R save(DepartmentAddBo bo) {
        if(bo !=null){
            Department department = new Department();
            BeanUtil.copyProperties(bo,department);
            //根据bo中的负责人姓名select负责人id；
//            Long doctorId = doctorDao.selectId(bo.getHeadName(), bo.getHeadPhone());
            Doctor doctor = doctorDao.selectOne(new QueryWrapper<Doctor>().eq("doctor_name", bo.getHeadName()).eq("doctor_phone", bo.getHeadPhone()));
            department.setHeadId(doctor.getId());
            if(dao.insert(department) > 0){
                return R.ok();
        }else {
                return R.fail();
            }
        }
        return R.fail("亲，新增异常！");
    }
    /**
    * 修改内容*/
    @Override
    public R update(DepartmentAddBo bo) {
        if (bo != null) {
            Department department = new Department();
            BeanUtil.copyProperties(bo, department);
            //根据bo中的负责人姓名select负责人id；
//            Long doctorId = doctorDao.selectId(bo.getHeadName(), bo.getHeadPhone());
            Doctor doctor = doctorDao.selectOne(new QueryWrapper<Doctor>().eq("doctor_name", bo.getHeadName()).eq("doctor_phone", bo.getHeadPhone()));
            department.setHeadId(doctor.getId());
            if (dao.updateById(department) > 0) {
                return R.ok();
            } else {
                R.fail();
            }

        }
        return R.fail("亲，修改异常！");
    }


    /**
    *删除 */
    @Override
    public R del(long id){
        if(id>0){
            int i=dao.deleteById(id);
            if(i>0){
                return R.ok();
            }
        }
        return R.fail("删除异常");
    }

    /**
    * 查询详情*/
    @Override
    public R queryById(long id){
        return R.ok(dao.selectById(id));
    }

    /**
    * 查询 所有数据*/
    @Override
    public R queryAll() {
      return R.ok(dao.selectList(null));
    }

    /**
     * 分页查询
     * @param bo 包括当前页、size和模糊查询数据
     * @return
     */
    @Override
    public R getDepartmentList(DepartmentPageBo bo) {
        String departName = bo.getDepartName();
        QueryWrapper<DepartmentDto> wrapper = new QueryWrapper<>();
        if(StringUtils.hasLength(departName)){
            wrapper.like("d1.depart_name",departName);
        }
        Page<DepartmentDto> page = new Page<>();
        page.setCurrent(bo.getCurrentPage());
        page.setSize(bo.getPageSize());
        Page<DepartmentDto> departmentPage = dao.page(page, wrapper);
        return R.ok(departmentPage);
    }


}
